package com.hand.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.hand.entity.Condition;
import com.hand.entity.Pagination;
import com.hand.entity.ResultModel;
import com.hand.entity.SsmItem;
import com.hand.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 86136 on 2019/7/19.
 */
@RestController
@ResponseBody
public class ItemController{
    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "selectItem",produces = "application/json")
    @ResponseBody
    public ResultModel selectItem(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "pageSize", defaultValue = "10" )Integer pageSize,
                                    @RequestParam (value="itemCode",required = false)String itemCode,
                                    @RequestParam(value = "itemDescription",required = false)String itemDescription,
                                    @RequestParam(value = "itemUom",required = false)String itemUom,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd") Date startActiveDate,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd") Date endActiveDate){

        //创建查询条件对象
        Condition condition = new Condition(itemCode,itemDescription,itemUom,startActiveDate,endActiveDate);
        System.out.println(condition);

        //获取总记录数
        int count = itemService.count(condition);
        System.out.println(count);
        if(currentPage==1) currentPage=1;

        //创建分页对象
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(pageSize);
        pagination.setTotalRecord(count);

        List<SsmItem> items = itemService.selectByCondition(condition,pagination);
        ResultModel resultModel = new ResultModel();
        resultModel.setSsmItems(items);
        resultModel.setTotal(count);

        System.out.println(items);
        return resultModel;
    }
    @RequestMapping("updateItem")
    public int updateItem(@RequestBody SsmItem ssmItem){
        System.out.println("update begin");
        return itemService.updateItem(ssmItem);
    }

    @RequestMapping(value = "insertItem")
    public int insertItem(@RequestBody SsmItem ssmItem){
        System.out.println(ssmItem);
        return itemService.insertItem(ssmItem);
    }

    @RequestMapping("deleteItem")
    public int deleteItem(@RequestParam(value = "itemId") BigInteger itemId){

        return itemService.deleteItem(itemId);
    }
}